package com.le.t1;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
 import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

public class client {
    private JFrame jFrame; //主界面
    private JPanel panel; //
    private JButton zc; //注册
    private JButton dl; //登录
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JTextField textField2;
    private Socket socket;
    private ServerSocket serverSocket;
    private InetAddress serverIP= null;
    private ObjectInputStream is =null;

    private ObjectOutputStream os=null;

    List<person> personList=new ArrayList<>();
    person person;

    public  client(){






        jFrame=new JFrame("主页");

        panel=new JPanel();
        JLabel label1=new JLabel("账号");
        JLabel label2=new JLabel("密码");
        JTextField textField1=new JTextField(50);
        JTextField textField2=new JTextField(50);
        zc=new JButton("注册");
        dl=new JButton("登录");
        jFrame.setSize(500,500);
        jFrame.setLocationRelativeTo(null);
        panel.setLayout(null);
        panel.setBounds(200,200,500,500);
        label1.setBounds(100,100,50,30);
        textField1.setBounds(150,100,100,30);
        label2.setBounds(100,130,50,30);
        textField2.setBounds(150,130,100,30);
        dl.setBounds(120,170,60,30);
        zc.setBounds(180,170,60,30);

        panel.add(label1);
        panel.add(label2);
        panel.add(dl);
        panel.add(zc);
        panel.add(textField1);
        panel.add(textField2);

        jFrame.add(panel);
        dl.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int n=-1;
                String t1=textField1.getText();
                String t2=textField2.getText();
                try {
                    serverSocket = new ServerSocket(7777);

                       new Serverdl();
                        socket = serverSocket.accept();

                        is = new ObjectInputStream(socket.getInputStream());
                        personList= (List<person>) is.readObject();
                        for (person person : personList){
                            if (t1.equals(person.getName()) && t2.equals(person.getPassword())){


                                n=1;
                            }
                        }
                        if (n==1){
                               JOptionPane.showMessageDialog(null," 账户存在,可以登录");
                        }else {
                            JOptionPane.showMessageDialog(null," 账户不存在,不可以登录");
                        }



                } catch (IOException e4) {
                    e4.printStackTrace();
                } catch (ClassNotFoundException classNotFoundException) {
                    classNotFoundException.printStackTrace();
                } finally {

                    try {
                        if(is!=null)
                            is.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        if(socket!=null)
                            socket.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                    try {
                        if(serverSocket!=null)
                            serverSocket.close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                }






            }
        });

        zc.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
              jFrame.setVisible(false);
              new zcjm();

            }
        });


       jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jFrame.setVisible(true);
    }

}
